<?php
namespace  app\admin\service;
class SmsService{

    public $language    = 'en';
    public $sp_id    = '627476';
    public $password = '38c9f31d';
    public function sendSms($mobile,$code,$event){
        /**
         * https://www.3yit.com
        账号：SPACE14715019766
        默认初始密码：Abc123456
         */
        //https://e.3yit.com/http-document?spid=627476&pwd=d3c8a574207f66a32c9516eecf5d0641#aes
        $this->language = request()->langset();
        $res = checkNoChineseAndLetters($mobile);;
        if($res['code'] != 0){
            return $res;
        }

        if($this->language == 'zh-hk'){
            $content = "【SPACESTUDIO】您正在注冊，驗證碼是:{$code},3分鐘内有效。";
        }else if($this->language == 'zh-en'){
            $content = "【SPACESTUDIO】您正在注册，验证码是:{$code},3分钟内有效。";
        }else{
            $content = "【SPACESTUDIO】You are registering,the verification code is:{$code},effective within 3 minutes";
        }

        if($event == 'resetpwd'){
            if($this->language == 'zh-hk'){
                $content = "【SPACESTUDIO】您正在重置密碼，驗證碼是:{$code}，3分鐘有效。";
            }else if($this->language == 'zh-en'){
                $content = "【SPACESTUDIO】您正在重置密码，验证码是:{$code}，3分钟有效。";
            }else{
                $content = "【SPACESTUDIO】You are resetting your password,the verification code is:{$code},effective within 3 minutes";
            }
        }

        $mobile = $res['data'];
        $apiurl = "https://api.3yit.com/api/send-sms-single";
        $data = [
            'sp_id'=>$this->sp_id,
            'mobile'=>$mobile,//85296983508
            'content'=>$content,
//            'signature'=>'1',
            'password'=>md5($this->password),
        ];
        $respon = $this->doCurl($data,$apiurl);
        return $respon;
    }
    function getBlance(){
        $apiurl = 'https://api.3yit.com/api/balance';
        $data = [
            'sp_id'=>$this->sp_id,
            'password'=>md5($this->password),
        ];
        $apiurl = $apiurl."?sp_id={$this->sp_id}&password={$data['password']}";
        $res = file_get_contents($apiurl);
        if(empty($res)){
            $reData = ['code'=>1005,'msg'=>'Server exception, try again later','data'=>''];//服务出错，请稍后再试
        }else{
            //{"code":0,"msg":"success","data":{"balance":"4.53"}}
            $reData = json_decode($res,true);

        }
        return $reData;
    }
    function checkMobile($mobile){
        $mobile = trim($mobile);
        $mobile = trim($mobile,'+');
        $sms_hk = config('global.sms_hk');
        if($sms_hk){
            //香港电话
            if(strlen($mobile) == 8){
                $mobile = "852{$mobile}";
                return ['code'=>0,'msg'=>'ok','data'=>$mobile];
            }else if(strlen($mobile) == 11){
                $pre = mb_substr($mobile,0,3);
                if($pre != '852'){
                    return ['code'=>1005,'msg'=>__('Only supports Hong Kong phone calls',[],$this->language),'data'=>$mobile];
                }else{
                    return ['code'=>0,'msg'=>'ok','data'=>$mobile];
                }
            }else{
                return ['code'=>1005,'msg'=>__('Only supports Hong Kong phone calls',[],$this->language),'data'=>$mobile];
            }
        }
        return ['code'=>0,'msg'=>'ok','data'=>$mobile];

    }
    function doCurl($data, $url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url );

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);

        curl_close($ch);
        if(empty($output)){
            return ['code'=>1005,'msg'=>'Server exception, try again later','data'=>''];
        }
        //{"code":-1,"msg":"","data":"WL:NMLJ"}  错误
        //{"code":0,"msg":"success","msg_id":100046647}  成功
        $result = json_decode($output,true);
        if($result['code'] == 0){
            $result['data'] = '';
            return $result;
        }

        trace_log(json_encode($result),'sms_send_error');
        $reData = ['code'=>1005,'msg'=>'Server non error','data'=>''];
        switch ($result['code']){
            case '10000':
                $reData = ['code'=>1005,'msg'=>'Server exception, try again later','data'=>''];//服务出错，请稍后再试
                break;
            case '10001':
                $reData = ['code'=>1005,'msg'=>'Params error','data'=>''];
                break;
            case '10008':
                $reData = ['code'=>1005,'msg'=>'Sms server account locked','data'=>''];//账号被锁定，请联系管理员确认
                break;
            case '10011':
                $reData = ['code'=>1005,'msg'=>'Sms server Insufficient credit limit','data'=>''];//额度不足
                break;
            case '10100':
                $reData = ['code'=>1005,'msg'=>'Server error','data'=>''];//签名校验失败
                break;
            case '10101':
                $reData = ['code'=>1005,'msg'=>'Server error','data'=>''];//您的ip不在白名单内
                break;
            case '10102':
                $reData = ['code'=>1005,'msg'=>'Server has error','data'=>''];//密码错误，请确认
                break;
            case '10200':
                $reData = ['code'=>1005,'msg'=>'Params is error','data'=>''];//产品sp_id必须填写
                break;
            case '10201':
                $reData = ['code'=>1005,'msg'=>'Phone number is not empty','data'=>''];//手机号不为空
                break;
            case '10202':
                $reData = ['code'=>1005,'msg'=>'SMS content must be filled in','data'=>''];//短信内容必须填写
                break;
            case '10203':
                $reData = ['code'=>1005,'msg'=>'SMS server has some error','data'=>''];//发送时间格式必须为UNIX时间戳
                break;
            case '10208':
                $reData = ['code'=>1005,'msg'=>'SMS server intercepted','data'=>''];//短信进拦截，具体原因参考data字段
                break;
            case '10209':
                $reData = ['code'=>1005,'msg'=>'System SMS too frequent','data'=>''];//触发防刷，请调整配置
                break;

        }

        return $reData;



    }
}